<?php

class GuestbookAction extends Action {

    public function index() {
        $Guestbook = D("Guestbook");
        $count = $Guestbook->count();
        import("ORG.Util.Page");
        $listRows = 5;
        $p = new page($count, $listRows);
        $list = $Guestbook->findAll(null, '*', 'addtime DESC', $p->firstRow . ',' . $p->listRows);
        $page = $p->show();
        if ($list !== false) {
            $this->assign('page', $page);
            $this->assign('list', $list);
        }
        $this->display();
    }

    public function add() {
        if (!$this->isPost()) {
            $this->redirect();
            return;
        } else {
            $verify = isset($_POST['verify']) ? trim($_POST['verify']) : '';
            if (md5($verify) != Session::get('verify')) {
                $this->assign('jumpUrl', __APP__ . '/Guestbook');
                $msg = "验证码错误或已经过期";
                $this->error($msg);
                return;
            }

            $title = isset($_POST['title']) ? trim($_POST['title']) : '';
            $username = isset($_POST['username']) ? trim($_POST['username']) : '';
            $email = isset($_POST['email']) ? trim($_POST['email']) : '';
            $qq = isset($_POST['qq']) ? trim($_POST['qq']) : '';
            $content = isset($_POST['content']) ? trim($_POST['content']) : '';

            if ($title == '') {
                $msg = "请填写留言标题";
                $this->error($msg);
                return;
            }
            if ($username == '') {
                $msg = "请填写用户姓名";
                $this->error($msg);
                return;
            }
            if ($email == '') {
                $msg = "请填写你的电子邮箱";
                $this->error($msg);
                return;
            }
            if ($content == '') {
                $msg = "请输入留言内容";
                $this->error($msg);
                return;
            }

            $data = array(
                'title' => $title,
                'username' => $username,
                'email' => $email,
                'qq' => $qq,
                'content' => $content,
            );
            $Guestbook = D("Guestbook");
            if (!$Guestbook->create($data)) {
                $this->error($Guestbook->getError());
                return;
            }
            if (!$Guestbook->add()) {
                $msg = "网站留言出错！";
                $this->error($msg);
                return;
            }
            $this->assign('jumpUrl', __URL__);
            $msg = "网站留言成功!";
            $this->success($msg);
        }
    }

    public function verify() {
        import("ORG.Util.Image");
        Image::buildImageVerify(4, 1, 'png', 48, 20, 'verify');
    }

}

?>